//
//  ZyaPurchaseCell.swift
//  SlaughterClient
//
//  Created by Zhouweibin on 2018/1/28.
//  Copyright © 2018年 zwb. All rights reserved.
//

import UIKit

class ZyaPurchaseCell: UITableViewCell {
    
    var deleteEvent: (() -> Void)?
    
    var model: ZyaFeedOrderList? {
        didSet {
            noLabel.text = model?.sOrderformNo
            nameLabel.text = "业务员：" + model!.sOperationNm.unwrap
            dateLabel.text = "下单时间：" + model!.sOrderformDate.unwrap
            amountLabel.text = "数量：" + model!.sAmountT.unwrap
            statusLabel.text = "是否预付款支付：" + (model!.advanceBs == "1" ? "是" : "否")
            ckLabel.text = model?.xgAuditMark
        }
    }
   
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        backgroundColor = Theme.backgroudColor
        selectionStyle = .none
        
        subview.backgroundColor = Theme.primaryColor()
        bgView.extRadius(radius: 5).extMaskBounds(mask: true)
        contentView.addSubview(bgView)
        bgView.addSubview(subview)
        bgView.addSubview(dateLabel)
        bgView.addSubview(noLabel)
        bgView.addSubview(amountLabel)
        bgView.addSubview(nameLabel)
        bgView.addSubview(statusLabel)
        bgView.addSubview(ckLabel)
        bgView.addSubview(deleteButton)
        
        setupConstraints()
        
        deleteButton.addTarget(self, action: #selector(deleteAction), for: .touchUpInside)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func deleteAction() {
        if let action = deleteEvent {
            action()
        }
    }
    
    private func setupConstraints () {
        
        bgView.snp.makeConstraints { (maker) in
            maker.edges.equalTo(UIEdgeInsets(top: 4, left: 8, bottom: 4, right: 8))
        }
        
        subview.snp.makeConstraints { (maker) in
            maker.right.left.top.equalToSuperview()
            maker.height.equalTo(30)
        }
        
        deleteButton.snp.makeConstraints { (maker) in
            maker.trailing.equalTo(subview.snp.trailing).inset(8)
            maker.size.equalTo(CGSize(width: 25, height: 25))
            maker.centerY.equalTo(subview)
        }
        
        noLabel.snp.makeConstraints { (maker) in
            maker.left.equalTo(8)
            maker.centerY.equalTo(subview.snp.centerY)
        }
        
        dateLabel.snp.makeConstraints { (maker) in
            maker.left.equalTo(8)
            maker.top.equalTo(noLabel.snp.bottom).offset(10)
        }
        
        amountLabel.snp.makeConstraints { (maker) in
            maker.left.equalTo(8)
            maker.top.equalTo(dateLabel.snp.bottom).offset(6)
        }
        
        nameLabel.snp.makeConstraints { (maker) in
            maker.left.equalTo(8)
            maker.top.equalTo(amountLabel.snp.bottom).offset(6)
        }
        
        ckLabel.snp.makeConstraints { (maker) in
            maker.right.equalToSuperview().inset(8)
            maker.centerY.equalTo(nameLabel)
            maker.height.equalTo(25)
        }
        
        statusLabel.snp.makeConstraints { (maker) in
            maker.left.equalTo(nameLabel)
            maker.top.equalTo(nameLabel.snp.bottom).offset(6)
            maker.bottom.equalToSuperview().offset(-8)

         }
    }
    
    lazy var bgView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.white
        return view
    } ()
    
    lazy var noLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.white
        label.font = UIFont.boldSystemFont(ofSize: 15)
        return label
    } ()
    
    lazy var deleteButton: UIButton = {
        let btn = UIButton()
        btn.setImage(#imageLiteral(resourceName: "del"), for: UIControl.State())
        return btn
    } ()
    
    lazy var dateLabel: UILabel = {
        let label = UILabel()
        label.textColor = Theme.textColor
        label.font = UIFont.systemFont(ofSize: 15)
        return label
    } ()
    
    lazy var amountLabel: UILabel = {
        let label = UILabel()
        label.textColor = Theme.textColor
        label.font = UIFont.systemFont(ofSize: 15)
        return label
    } ()
    
    lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.textColor = Theme.textColor
        label.font = UIFont.systemFont(ofSize: 15)
        return label
    } ()
    
    lazy var statusLabel: UILabel = {
        let label = UILabel()
        label.textColor = Theme.textColor
        label.font = UIFont.systemFont(ofSize: 15)
        return label
    } ()
    
    lazy var ckLabel: UILabel = {
        let label = UILabel()
        label.backgroundColor = Theme.tableViewColor
        label.layer.cornerRadius = 5
        label.textColor = Theme.blue
        label.layer.masksToBounds = true
        label.textAlignment = .center
        label.font = UIFont.systemFont(ofSize: 14)
        return label
    } ()
    
    lazy var subview: UIView = {
        let view = UIView()
        view.backgroundColor = Theme.primaryLightColor
        return view
    } ()
    
}
